//
//  IdeaViewController.m
//  HappinessTrip
//
//  Created by dllo on 15/10/22.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "IdeaViewController.h"
#import "SKPSMTPMessage.h"
#import "NSData+Base64Additions.h"
#import "Model.h"
#import "Fit.h"

@interface IdeaViewController ()<SKPSMTPMessageDelegate, UITextFieldDelegate>
@property(nonatomic, retain)UITextField *textField;

@end

@implementation IdeaViewController
-(void)dealloc
{
    [_textField release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [Model shareInstance].BackgroundColor;
    
}
-(void)changeColor{
    self.view.backgroundColor = [Model shareInstance].BackgroundColor;
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[Model shareInstance].TitleColor, NSForegroundColorAttributeName, nil]];
    self.navigationController.navigationBar.barTintColor = [Model shareInstance].BackgroundColor;
    self.tabBarController.tabBar.backgroundColor = [Model shareInstance].BackgroundColor;
}


-(void)loadView
{
    [super loadView];
    self.textField = [[UITextField alloc]initWithFrame:CGRectMake(50 * W, 50 * H, 275 * W, 200 * H)];
    _textField.placeholder = @"请输入意见";
    _textField.borderStyle = UITextBorderStyleRoundedRect;
    _textField.delegate = self;
    _textField.clearButtonMode = UITextFieldViewModeWhileEditing;//输入框中是否有个叉号，在什么时候显示，用于一次性删除输入框中的内容
    _textField.returnKeyType = UIReturnKeySend;//键盘return键变成什么键
    [self.view addSubview:_textField];
    [_textField release];
    
    [self changeColor];
    
}

-(void)viewWillDisappear:(BOOL)animated
{
    [_textField resignFirstResponder];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (_textField.text.length == 0) {
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"温心提示"message:@"请输入意见"preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}];
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
    SKPSMTPMessage *myMessage = [[SKPSMTPMessage alloc]init];
    myMessage.fromEmail = @"rwzrwz449@163.com";
    myMessage.toEmail = @"2606568887@qq.com";
    myMessage.relayHost = @"smtp.163.com";
    myMessage.requiresAuth = YES;
    if (myMessage.requiresAuth) {
        myMessage.login =  @"rwzrwz449@163.com";
        myMessage.pass = @"rwzrwz449@";
    }
    myMessage.wantsSecure = YES;
    myMessage.subject = @"123";
    myMessage.delegate = self;
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain;charset = UTF-8", kSKPSMTPPartContentTypeKey, _textField.text, kSKPSMTPPartMessageKey, @"8bit", kSKPSMTPPartContentTransferEncodingKey, nil];
    myMessage.parts = [NSArray arrayWithObjects:dic, nil];
    [myMessage send];
    
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"温心提示"message:@"发送成功.感谢您提出的宝贵意见,我们会努力做得更好!"preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {}];
    [alert addAction:defaultAction];
    [self presentViewController:alert animated:YES completion:nil];
    
    [_textField resignFirstResponder];
    return YES;
}

-(void)messageSent:(SKPSMTPMessage *)message
{
//    NSLog(@"发送成功");
}
-(void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error
{
//    NSLog(@"发送失败");
}











- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
